"""
 py2登录：
            1、输入账号、密码
            2、账号输入的不存在要提示它
            3、最多输入3次
            4、账号长度6-12，密码长度8-12
            5、登录成功程序结束，print欢迎xxx登陆，今天的日期是xxx
"""
import datetime

user_dic = {}
count = 0

def read_file_userinfo(path):
    with open(path, encoding='utf-8') as username_file:
        userinfos = username_file.readlines()
        for userinfo in userinfos:
            # 去掉换行符，以，分割
            userinfo = userinfo.strip().split(",")
            user_dic[userinfo[0]] = userinfo[1]
    return user_dic

def check_userinfo(username, password):
    user_dic = read_file_userinfo(path='user.txt')
    now = datetime.datetime.today()
    global count
    if username not in user_dic.keys():
        print("您输入的用户名不存在或者密码错误,您当前可输入次数%d"%(2-count))
        count+=1
        if count>=3:
            print("账号锁定")
        else:
            login()

    elif str(password) == user_dic[username]:
        print("欢迎%s登录，当前时间是%s" % (username, now))

def login():
    username = input("请输入用户名:")
    password = input("请输入密码:")
    check_userinfo(username, password)


if __name__ == '__main__':
    login()
